package com.cat.model.web.action.admin.reservation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.service.admin.SolutionService;
import com.cat.model.common.model.common.PlmCityVO;
import com.cat.model.common.model.common.SolutionModelVO;
import com.cat.model.common.model.common.SolutionVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;
import com.cat.model.web.bean.SolutionModelBean;

public class SolutionManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;

	private SolutionVO solution;
	private Long id;
	private String reservationId;
	private String solutionId;
	private SolutionModelBean solutionModelBean;
	private SolutionModelVO solutionModel;
		
	@Autowired
	private SolutionService solutionService; 
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	public String solution() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(reservationId != null){
			if(reservationId.contains(",")){
				String[] strs = reservationId.split(",");
				reservationId = strs[0];
			}
		}
		
		SolutionVO solution = modelMapper.getSolution(new Long(reservationId));
		if(solution==null){
			solution = solutionService.createInitSolution(new Long(reservationId));
		}
		
		request.setAttribute("solution", solution);
		List<PlmCityVO> cityList = plmCommDAO.getAllPlmCity();
		request.setAttribute("cityList", cityList);
		
		condition.put("solutionId", solution.getId());
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchSolutionModelSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<SolutionModelBean> list = (List<SolutionModelBean>)plmCommDAO.search("manager.searchSolutionModel", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "solution";
	}
	
	public String save(){
		try {
			solutionService.createSolution(solution);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "solutionList";
		}
		parentAlert(true, "保存成功");
		return "solutionList";
	}
	
	public String deleteSingle() {
		SolutionVO solution = modelMapper.getSolution(new Long(reservationId));
		if(solution==null){
			parentAlert(false, "该预约单不存在方案!");
			return "solutionList";
		}
		solutionService.deleteSolution(solution.getId());
		okAlert("删除成功！");
		return "solutionList";
	}
	
	public String deleteSingleModel() {
		solutionService.deleteSolutionModel(id);
		okAlert("删除成功！");
		return "solutionList";
	}
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings({ "unchecked" })
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(solutionId != null){
			if(solutionId.contains(",")){
				String[] strs = solutionId.split(",");
				solutionId = strs[0];
			}
		}
		
		condition.put("solutionId", solutionId);
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchSolutionModelSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<SolutionModelBean> list = (List<SolutionModelBean>)plmCommDAO.search("manager.searchSolutionModel", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "solutionList";
	}
	
	public String toAdd(){
		if(StringUtils.isEmpty(solutionId)){
			parentAlert(false, "请先保存编辑的方案");
			return "solutionList";
		}
		return "modelAdd";
	}
	
	public String modelAdd(){
		try {
			String modelId = request.getParameter("model.id");
			solutionModel.setModelId(new Long(modelId));
			
			if(solutionModel.getWorkStartTime()!=null&&solutionModel.getWorkEndTime()!=null){
				if(solutionModel.getWorkStartTime().compareTo(solutionModel.getWorkEndTime())>0){
					parentAlert(false, "工作开始时间不能晚于工作结束时间");
					return "solutionList";
				}
				List<SolutionModelVO> modelList = modelMapper.getSolutionModelByType(solutionModel.getSolutionId(), solutionModel.getModelId());
				if(modelList!=null&&modelList.size()>0){
					for (int i = 0; i < modelList.size(); i++) {
						SolutionModelVO modelVO = modelList.get(i);
						if((modelVO.getWorkStartTime().compareTo(solutionModel.getWorkEndTime())<0&&modelVO.getWorkEndTime().compareTo(solutionModel.getWorkStartTime())>0)){
							parentAlert(false, "该方案中本模特的工作时间与已有工作时间有重叠，请确认");
							return "solutionList";
						}
					}
				}	
			}
			
			solutionService.createSolutionModel(solutionModel);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "solutionList";
		}
		parentAlert(true, "保存成功");
		return "solutionList";
	}
	
	public String toEdit(){
		solutionModelBean = modelMapper.getSolutionModelBean(id);
		return "modelEdit";
	}
	
	public String modelEdit(){
		try {
			String modelId = request.getParameter("model.id");
			solutionModel.setModelId(new Long(modelId));
			
			if(solutionModel.getWorkStartTime()!=null&&solutionModel.getWorkEndTime()!=null){
				if(solutionModel.getWorkStartTime().compareTo(solutionModel.getWorkEndTime())>0){
					parentAlert(false, "工作开始时间不能晚于工作结束时间");
					return "solutionList";
				}
				List<SolutionModelVO> modelList = modelMapper.getSolutionModelByType(solutionModel.getSolutionId(), solutionModel.getModelId());
				if(modelList!=null&&modelList.size()>0){
					for (int i = 0; i < modelList.size(); i++) {
						SolutionModelVO modelVO = modelList.get(i);
						if(!modelVO.getId().equals(solutionModel.getId())&&
								(modelVO.getWorkStartTime().compareTo(solutionModel.getWorkEndTime())<0&&modelVO.getWorkEndTime().compareTo(solutionModel.getWorkStartTime())>0)){
							parentAlert(false, "该方案中本模特的工作时间与已有工作时间有重叠，请确认");
							return "solutionList";
						}
					}
				}
			}
			
			solutionService.updateSolutionModel(solutionModel);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "solutionList";
		}
		parentAlert(true, "保存成功");
		return "solutionList";
	}

	public SolutionVO getsolution() {
		return solution;
	}

	public void setsolution(SolutionVO solution) {
		this.solution = solution;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public SolutionModelBean getSolutionModelBean() {
		return solutionModelBean;
	}

	public void setSolutionModelBean(SolutionModelBean solutionModelBean) {
		this.solutionModelBean = solutionModelBean;
	}

	public SolutionVO getSolution() {
		return solution;
	}

	public void setSolution(SolutionVO solution) {
		this.solution = solution;
	}

	public String getSolutionId() {
		return solutionId;
	}

	public void setSolutionId(String solutionId) {
		this.solutionId = solutionId;
	}

	public String getReservationId() {
		return reservationId;
	}

	public void setReservationId(String reservationId) {
		this.reservationId = reservationId;
	}

	public SolutionModelVO getSolutionModel() {
		return solutionModel;
	}

	public void setSolutionModel(SolutionModelVO solutionModel) {
		this.solutionModel = solutionModel;
	}

}
